const router = require('koa-router')()
const {getHouse, getHouseById, patchHouse, postHouse, deleteHouse, getHouseMy, patchHouseStatus, getHouseShops, getHouseMyById} = require('../../controller/house')

router.prefix('/api/house')

router.get('/house', async (ctx, next) => {
  const {name, address, isSchool, minRent, maxRent, schoolId, status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  const areaId = ctx.query['areaId[]']
  ctx.body = await getHouse({
    name,
    address,
    minRent,
    maxRent,
    areaId,
    isSchool,
    schoolId,
    status,
    createTime,
    pageIndex,
    pageSize
  })
})

router.get('/house/my', async (ctx, next) => {
  const {name, address, isSchool, minRent, maxRent, schoolId, status, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  const areaId = ctx.query['areaId[]']
  ctx.body = await getHouseMy(ctx, {
    name,
    address,
    minRent,
    maxRent,
    areaId,
    isSchool,
    schoolId,
    status,
    createTime,
    pageIndex,
    pageSize
  })
})

router.get('/house/shops', async (ctx, next) => {
  const {name, address, minRent, maxRent, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  const areaId = ctx.query['areaId[]']
  ctx.body = await getHouseShops(ctx, {name, address, minRent, maxRent, createTime, areaId, pageIndex, pageSize})
})

router.get('/house/my/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await getHouseMyById(id)
})

router.get('/house/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await getHouseById(id)
})

router.post('/house', async (ctx, next) => {
  const {name, address, rent, imgs, description, areaId, isSchool, schoolId, status} = ctx.request.body
  ctx.body = await postHouse(ctx, {name, address, rent, imgs, description, areaId, isSchool, schoolId, status})
})

router.patch('/house', async (ctx, next) => {
  const {name, address, rent, imgs, description, areaId, isSchool, schoolId, id} = ctx.request.body
  ctx.body = await patchHouse({name, address, rent, imgs, description, areaId, isSchool, schoolId, id})
})

router.patch('/house/status', async (ctx, next) => {
  const {status, id} = ctx.request.body
  ctx.body = await patchHouseStatus({status, id})
})


router.delete('/house/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await deleteHouse(id)
})

module.exports = router
